package com.mission.schedule.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * 下载和同步清单
 * Created by liwei on 2017/3/2.
 */

public class NoteShareService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
        App.getHttpQueues().cancelAll("downf");
    }


    SharedPrefUtil sharedPrefUtil;
    String UserId = "";
    String downtime = "2016-01-01";
    String maxTitleId = "";
    String title = "";
    int uid = 0;
    String remark5 = "0";
    String remark6 = "0";
    String titles = "";

    @Override
    public void onStart(final Intent intent, int startId) {
        super.onStart(intent, startId);

        sharedPrefUtil = new SharedPrefUtil(getApplication(),
                ShareFile.USERFILE);
        UserId = sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.USERID, "");
        downtime = sharedPrefUtil.getString(
                getApplication(), ShareFile.USERFILE,
                ShareFile.UPDATENOTESETTIME, "2017-01-01%2B00:00:00");
        maxTitleId = intent.getStringExtra("maxTitleId");
        uid = intent.getIntExtra("uid",0);
        remark5 = intent.getStringExtra("remark5");
        remark6 = intent.getStringExtra("remark6");
        titles = intent.getStringExtra("titles");
        sysNewFriend();
    }



    /**
     * 清单分享
     */
    public void sysNewFriend() {
        final List<NoteTitleDetailBean.ListBean> list = App.getDBcApplication().getTitleNoteDetailData(UserId, maxTitleId, false);
        if (list.size() > 0) {
            JSONObject jsonObject = new JSONObject();
            try {
                JSONArray jsonArray = new JSONArray();
                String titleid = DateUtil.nowTime1();
                for (NoteTitleDetailBean.ListBean bean : list) {
                    JSONObject js = new JSONObject();
                    js.put("id", bean.id);
                    js.put("titleId", titleid);
                    js.put("uid", 0);
                    js.put("titles", titles);
                    js.put("imgPath", bean.imgPath);
                    js.put("imgUrl", bean.imgUrl);
                    js.put("shareUrl", bean.shareUrl);
                    js.put("contents", bean.contents);
                    js.put("nums", bean.nums);
                    js.put("style", bean.style);
                    js.put("endstate", bean.endstate);
                    js.put("lType", bean.lType);
                    js.put("orderId", bean.orderId);
                    js.put("createTime", bean.createTime);
                    js.put("changeTime", bean.changetime);
                    js.put("cpath", bean.cpath);
                    js.put("curl", bean.curl);
                    js.put("remark", bean.remark);
                    js.put("remark1", bean.remark1);
                    js.put("remark2", bean.remark2);
                    js.put("remark3", remark5);
                    js.put("remark4", remark6);
                    js.put("remark5", bean.remark3);
                    js.put("remark6", bean.remark4);
                    js.put("puid", UserId);
                    js.put("pname", sharedPrefUtil.getString(getApplicationContext(),
                            ShareFile.USERFILE, ShareFile.USERNAME, ""));
                    js.put("states", 0);
                    js.put("sends", 0);
                    js.put("pimg", sharedPrefUtil.getString(getApplicationContext(),
                            ShareFile.USERFILE, ShareFile.USERPHOTOPATH, ""));
                    jsonArray.put(js);
                }
                jsonObject.put("classDownload", jsonArray);

                JSONArray uidarray = new JSONArray();
                JSONObject uidob = new JSONObject();
                uidob.put("uId",uid);
                uidarray.put(uidob);
                jsonObject.put("uIds", uidarray);

                HttpUtils httpUtils = new HttpUtils(10000);
                RequestParams params = new RequestParams();
                params.addBodyParameter("data", jsonObject.toString());
                params.addBodyParameter("row1","▶︎ @"+sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.USERNAME, "")+" 给你转发了一个清单");
                params.addBodyParameter("row2","#"+ titles+"#@" + sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.USERNAME, ""));
                httpUtils.send(HttpMethod.POST, URLConstants.清单转发, params,
                        new RequestCallBack<String>() {

                            @Override
                            public void onFailure(HttpException e, String msg) {
                                Log.e("TAG", "清单转发失败" + msg);
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                if (!TextUtils.isEmpty(responseInfo.result)) {
                                    Log.e("TAG", "清单转发:" + responseInfo.result);
                                }
                            }
                        });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
